<template>
  <div class="app-container">
    <!-- 查询区域 -->
    <el-form :model="queryParams" v-show="showSearch" inline label-width="auto" @keyup.enter.native="handleQuery">
      <el-form-item label="操作端" >
        <lam-dict-select v-model="queryParams.operatorType" dictCode="operator_type" :useEmptyVal="false" @change="handleQuery" :clearable="false"/>
      </el-form-item>
      <el-form-item label="关键字">
        <span slot="label">
          关键字<el-tooltip content="支持登录账号、昵称、真实姓名、邮箱、电话的关键字查询" placement="top"><i class="el-icon-question"></i></el-tooltip>
        </span>
        <el-input v-model="queryParams.keywords" placeholder="请输入关键字" clearable />
      </el-form-item>
      <el-form-item label-width="0px">
        <el-button type="primary" icon="el-icon-search"  @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh"  @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="tool-bar-btn">
      <el-button type="danger" icon="el-icon-delete"  :disabled="multiple" @click="handleForceLogout"
              v-hasPerms="['monitor:onlineUser:forceLogout']">强制退出</el-button>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </div>

    <!-- 列表区域 -->
    <selected-info :count="selectedRows.length" :onClearSelected="onClearSelected" />
    <el-table :ref="dataTableRef" v-loading="loading" :data="dataList" stripe border @selection-change="onSelectChange"
          :default-sort = "defaultSort" @sort-change="onSortChange" :row-key="rowKey" >
      <el-table-column type="selection" width="55" align="center" fixed="left"/>
      <el-table-column label="编号" align="center" prop="no" :ellipsis="true" width="80px"/>
      <el-table-column label="登录账号" align="center" prop="username" :ellipsis="true" width="200px" >
        <template slot-scope="{row}">
          <el-tag v-if="row.curUser" >{{ row.username }}</el-tag>
          <span v-else>{{row.username}}</span>
        </template>
      </el-table-column>
      <el-table-column label="昵称" align="center" prop="nickName" width="100px"/>
      <el-table-column label="真实姓名" align="center" prop="realname" width="100px"/>
      <el-table-column label="操作端" align="center" prop="operatorTypeDictText" :ellipsis="true" width="100px" />
      <el-table-column v-if="queryParams.operatorType=='f'" label="当前操作公司" align="center" prop="curCompanyCodeDictText" width="150px" />
      <el-table-column label="性别" align="center" prop="sexDictText" :ellipsis="true" width="80px" />
      <el-table-column label="邮箱" align="center" prop="email" :ellipsis="true" width="180px" />
      <el-table-column label="电话" align="center" prop="phone" :ellipsis="true" width="150px" />
      <el-table-column label="状态" align="center" prop="statusDictText" width="80">
        <template slot-scope="{row}"><UserStatus :value="row.status" :text="row.statusDictText" /></template>
      </el-table-column>
      <el-table-column label="用户类型" align="center" prop="userTypeDictText" :ellipsis="true" width="150px" />
      <el-table-column label="登录类型" align="center" prop="regTypeDictText" :ellipsis="true" width="150px" />
      <el-table-column label="登录时间" align="center" prop="loginTimeStr" :ellipsis="true" width="160px" />
      <el-table-column label="在线时长" align="center" prop="onlineTimeStr" :ellipsis="true" width="200px" />
      <el-table-column label="token有效时长" align="center" prop="expireTimeStr" :ellipsis="true" width="180px" />
      <el-table-column label="过期时间" align="center" prop="expiresAtStr" :ellipsis="true" width="160px" />
      <el-table-column label="登录IP" align="center" prop="ipaddr" width="150px" />
      <el-table-column label="客户端" align="center" prop="browser" width="150px" />
      <el-table-column label="操作系统" align="center" prop="os" width="150px" />
      <el-table-column align="center" label="操作" fixed="right" width="200px">
        <template slot-scope="{row}">
          <el-button type="text" icon="info" class="linkBtn" @click="handleDetail(row)" >详情</el-button>
          <el-button type="text" v-if="!row.curUser" icon="el-icon-delete" @click="handleForceLogout(row)"
              v-hasPerms="['monitor:onlineUser:forceLogout']">强制退出</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="!loading" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"  @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <UserOnlineInfo ref="formDialog" />

  </div>
</template>

<script>
import {httpGet, httpDel} from '@/api';
import { IndexListMixin } from '@/components/mixins/IndexListMixin';
import UserOnlineInfo from './detail';

export default {
  name: "OnlineUser",
  mixins:[IndexListMixin],
  components: {
    UserOnlineInfo,
  },
  data() {
    return {
      rowKey: 'tokenId',
      // 查询参数
      queryParams: {
        operatorType: 'adm',
        keywords: '',
        pageNum: 1,
        pageSize: 10
      },
      url: {
      	list: '/monitor/onlineUser/list',
      	forceLogout: '/monitor/onlineUser/'
      }
    };
  },
  created() {
    this.getList();
  },
  mounted(){
  },
  methods: {
    getList() {
      this.loadData(this.url.list);
    },

    /** 查看详情按钮操作 */
    handleDetail(row) {
      this.$refs.formDialog.toDetail("在线用户详情", row);
    },

    /** 删除按钮操作 */
    handleForceLogout(row) {
      const ids = row.tokenId || this.selectedRowKeys.join()
      this.$confirm('确认踢出所选用户?', "警告", {type: "warning"}).then(()=>{
        httpDel(this.url.forceLogout + ids).then((res) => {
          this.getList();
          this.msgSuccess("踢出成功！");
        }).catch(function() {});
      })
    },


  }
};
</script>
